package com.zoe.onelink.business.util.ws;


import cn.hutool.core.thread.ThreadUtil;
import com.zoe.onelink.util.ValidUtil;

import java.util.Map;
import java.util.concurrent.ExecutorService;

/**
 * <p>标题: 请求第三方webService接口帮助类</p>
 * <p>描述: 同步、异步</p>
 * <p>版权: Copyright (c) 2018</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: cxy
 * @date: 2018/5/4
 */
@Deprecated
public class HttpRequestHelper {

    /**
     * 默认执行写入线程数
     */
    private static final int DEFAULT_WRITE_THREADS = 10;

    /**
     * 线程池
     */
    private static ExecutorService pool = ThreadUtil.newExecutor(DEFAULT_WRITE_THREADS);

    /**
     * 执行调用(同步)
     *
     * @param url     url地址
     * @param headers 请求头
     * @param params  请求体
     * @return String 返回信息
     * @throws Exception 异常信息
     */
    public static String doSyn(String url, Map<String, String> headers, Map<String, String> params) throws Exception {
        if (ValidUtil.isEmptyOrNull(url)) {
            throw new Exception("api接口地址不能为空");
        }
        return HttpRequestUtil.doRequest(url, headers, params);
    }

    /**
     * 执行调用(同步)
     *
     * @param url         url地址
     * @param headers     请求头
     * @param params      请求体
     * @param readTimeOut 超时时间 单位毫秒
     * @return String 返回信息
     * @throws Exception 异常信息
     */
    public static String doSyn(String url, Map<String, String> headers, Map<String, String> params, int readTimeOut) throws Exception {
        if (ValidUtil.isEmptyOrNull(url)) {
            throw new Exception("api接口地址不能为空");
        }
        return HttpRequestUtil.doRequest(url, headers, params, readTimeOut);
    }

    /**
     * 执行调用(异步)
     *
     * @param url     url地址
     * @param headers 请求头
     * @param params  请求体
     * @throws Exception 异常信息
     */
    public static void doAsyn(String url, Map<String, String> headers, Map<String, String> params) throws Exception {
        if (ValidUtil.isEmptyOrNull(url)) {
            throw new Exception("api接口地址不能为空");
        }
        pool.submit(new AsynTask(url, headers, params));
    }

    /**
     * 异步调用任务
     */
    private static class AsynTask implements Runnable {
        // api接口地址
        private String url;
        // 请求头
        private Map<String, String> headers;
        // 请求体
        private Map<String, String> params;
        // 超时时间
        private int readTimeOut = 0;

        AsynTask(String url, Map<String, String> headers, Map<String, String> params) {
            this.url = url;
            this.headers = headers;
            this.params = params;
        }

        AsynTask(String url, Map<String, String> headers, Map<String, String> params, int readTimeOut) {
            this.url = url;
            this.headers = headers;
            this.params = params;
            this.readTimeOut = readTimeOut;
        }

        @Override
        public void run() {
            try {
                if (0 == readTimeOut) {
                    HttpRequestUtil.doRequest(url, headers, params);
                } else {
                    HttpRequestUtil.doRequest(url, headers, params, readTimeOut);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
